package springboard.auth.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import springboard.auth.bean.Role;

import java.util.List;

@Mapper
public interface RoleMapper extends BaseMapper<Role> {

    @Select("select * from role where `key`=#{key}")
    Role selectById(@Param("key") String key);

    @Select("select * from role")
    List<Role> selectAll();

    @Insert("insert into role(`key`, label) values(#{key}, #{label})")
    int insert(Role role);

    @Update("update role set `key`=#{role.key}, label=#{role.label} where `key`=#{oldKey}")
    int update(Role role, @Param("oldKey") String oldKey);

    @Select("select permission_key from role__permission where role_key=#{roleKey}")
    List<String> selectRolePermissionKeys(@Param("roleKey") String roleKey);

    @Insert("insert ignore into role__permission(role_key, permission_key) values(#{roleKey}, #{permissionKey})")
    int insertRolePermission(@Param("roleKey") String roleKey, @Param("permissionKey") String permissionKey);

    @Delete("delete from role__permission where role_key=#{roleKey} and permission_key=#{permissionKey}")
    int deleteRolePermission(@Param("roleKey") String roleKey, @Param("permissionKey") String permissionKey);

    @Insert("delete from role__permission where role_key=#{roleKey}")
    int deleteAllRolePermission(@Param("roleKey") String roleKey);

    @Update("update user__role set role_key=#{newKey} where role_key=#{oldKey}")
    int updateAllUserRole(String newKey, String oldKey);

    @Update("update role__permission set role_key=#{newKey} where role_key=#{oldKey}")
    int updateAllRolePermission(String newKey, String oldKey);

    @Delete("delete from role where `key`=#{key}")
    int delete(@Param("key") String key);

    @Delete("delete from user__role where role_key=#{roleKey}")
    int deleteAllUserRole(@Param("roleKey") String roleKey);

}
